﻿namespace CommLib
{
    public class MessageConversations : BluetoothMessage
    {
        public MessageConversations(Conversation[] list)
		    : base(BtMessageType.ThreadList, null)
        {
		    mData = new string[list.Length * Conversation.FIELD_COUNT];

		    int i = 0;
		    foreach (Conversation cv in list) {
			    mData[i++] = cv.getPhoneNumber();
			    mData[i++] = cv.getContactName();
			    mData[i++] = cv.getThreadId().ToString();
			    mData[i++] = cv.getMessageCount().ToString();
			    mData[i++] = cv.getSnippet();
		    }
	    }

        public MessageConversations(byte[] data, int length)
		    : base(BtMessageType.ThreadList, data, length)
        {
	    }

	    public Conversation[] getConversations() {
		    Conversation[] list = null;

		    if (mData.Length % Conversation.FIELD_COUNT == 0) {
			    list = new Conversation[mData.Length / Conversation.FIELD_COUNT];

                int idx = 0;
			    for (int i = 0; i < mData.Length; i += Conversation.FIELD_COUNT) {
				    list[idx++] = new Conversation(mData, i);
			    }
		    }

		    return list;
	    }
    }
}
